package xkp.lesson6;

import java.util.ArrayList;
import java.util.LinkedList;

public class Product extends Thread{
    private LinkedList<Commonity> list;

    public Product(LinkedList<Commonity> list) {
        this.list = list;
    }

    @Override
    public void run() {
        int num = 0;
        Thread.currentThread().setName("Product");
        while(true){
            synchronized (list) {
                if (list.size() >= 500) {
                    try {
                        list.notifyAll();
                        list.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                } else {
                    Commonity commonity = new Commonity(++num);
                    list.push(commonity);
                    System.out.println(Thread.currentThread().getName()+":"+list.size());
                }
            }

            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
